<?php

namespace api\controller;

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\NoteService;
use common\validate\Note;
use support\ErrorCode;
use think\Request;
use think\Exception;
use think\Response;

class NoteController extends VerifyController
{
    /**
     * @var \common\service\NoteService
     */
    protected $noteService;

    public function __construct()
    {
        parent::__construct();
        $this->noteService = NoteService::getInstance();
    }

    /**
     * 创建Note
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $data = $param['data'];

        $validate = new Note();
        if (!empty($data["note_entity_relation"])) {
            foreach ($data["note_entity_relation"] as $noteEntityRelationItemData) {
                $validate->scene('AddNoteEntityRelationBase');
                if (!$validate->check($noteEntityRelationItemData)) {
                    throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
                }
                // 根据类型走不同的验证条件
                if (in_array($noteEntityRelationItemData['relation_type'], ['episode', 'entity', 'step_category', 'step', 'scene'])) {
                    $sv = $validate->scene('AddNoteEntityRelation');
                } else {
                    $sv = $validate->scene('AddNoteEntityRelationTodo');
                }
                if (!$sv->check($noteEntityRelationItemData)) {
                    throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
                }
            }
        }

        try {
            $resData = $this->noteService->createNote($data);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询反馈 包含用户详情 部门 角色信息
     * @param Request $request
     * @return Response
     * @throws \think\Exception
     */
    public function selectWithUserDetail(Request $request)
    {
        $param = $request->param();
        $resData = $this->noteService->selectWithUserDetail($param['param'], $request->getTenantId());
        return json(success_response('', $resData));
    }

    /**
     * 获取版本角色
     * @param Request $request
     * @return Response
     * @throws \think\Exception
     */
    public function getVersionNote(Request $request)
    {
        $param = $request->param()['param'];
        $versionId = $param['filter']['version_id'];
        $page = $param['page'] ?? [1, C('default_page_total')];
        $resData = $this->noteService->getVersionNote($versionId, $request->getTenantId(), $page);
        return json(success_response('', $resData));
    }


    /**
     * 获取需求列表
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getDemandNoteList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->noteService->getDemandNoteList($param);
        return json(success_response('', $resData));
    }

    /**
     * 通过指定任务ids获取最新一条note数据
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getLastDemandNoteListTextByTaskIds(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->noteService->getLastDemandNoteListTextByTaskIds($param['filter'], $param['fields']);
        return json(success_response('', $resData));
    }

    /**
     * 获取需求的详细信息
     * @param Request $request
     * @return Response
     */
    public function demandInformation(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->noteService->demandInformation($param["filter"]);
        return json(success_response('', $resData));
    }

    /**
     * 获取需求实体列表
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function getDemandEntityList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->noteService->getDemandEntityList($param["filter"]);
        return json(success_response('', $resData));
    }

    /**
     * 删除需求实体关联关系
     * @param Request $request
     * @return Response
     */
    public function delDemandRelationEntity(Request $request)
    {
        $param = $request->param()['param'];
        $id = $param["filter"]['id'];
        $resData = $this->noteService->delDemandRelationEntity($id);
        return json(success_response('', $resData));
    }

    /**
     * 更新note
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function updateNote(Request $request)
    {
        $param = $request->param();
        $data = $param['data'];
        $validate = new Note();
        if (!empty($data["note_entity_relation"])) {
            $validate->scene('AddNoteEntityRelation');
            foreach ($data["note_entity_relation"] as $noteEntityRelationItemData) {
                if (!$validate->check($noteEntityRelationItemData)) {
                    throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
                }
            }
        }

        try {
            $resData = $this->noteService->updateNote($data);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取会话需求列表
     * @param Request $request
     * @return Response
     */
    public function getChatDemandList(Request $request)
    {
        $param = $request->param()['param'];
        if (empty($param["filter"]["note_entity_relation.entity_id"])) {
            throw_strack_exception('note_entity_relation.entity_id require', ErrorCode::REQUEST_PARAM_ERROR);
        }
        $resData = $this->noteService->getChatDemandList($param);
        return json(success_response('', $resData));
    }

    /**
     * 获取任务的需求列表
     * @param Request $request
     * @return Response
     */
    public function getTaskDemandNoteList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->noteService->getTaskDemandNoteList($param);
        return json(success_response('', $resData));
    }

    /**
     * 获取实体的需求列表
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getEntityDemandNoteList(Request $request)
    {
        $param = $request->param()['param'];
        $resData = $this->noteService->getEntityDemandNoteList($param);
        return json(success_response('', $resData));
    }

    /**
     * 获取需求列表
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $request->param()['param']['page'] ?? [1, C('default_page_total')];
        $resData = $this->noteService->getList($param, $page, $request->getModuleCode());
        return json(success_response('', $resData));
    }

    /**
     * 获取剧集下的镜头
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getEpisodeShot(Request $request)
    {
        $param = $request->param()['param'];
        $page = $request->param()['param']['page'] ?? [1, C('default_page_total')];
        $resData = $this->noteService->getEpisodeShot($param, $page);
        return json(success_response('', $resData));
    }

    /**
     * 删除需求
     * @param \think\Request $request
     * @return Response
     */
    public function deleteDemandNote(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $resData = $this->noteService->deleteDemandNote($filter);
        return json(success_response('', $resData));
    }

    /**
     * 查询资产需求
     * @param Request $request
     * @return Response
     */
    public function getAssetNote(Request $request)
    {
        $param = $request->param()['param'];
        $entityIds = $param['filter']['entity_ids'];
        $resData = $this->noteService->getAssetNote($entityIds, true);
        return json(success_response('', $resData));
    }

    /**
     * 获取资产关联需求列表
     * @param Request $request
     * @return Response
     */
    public function getAssetRelationNoteList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $request->param()['param']['page'] ?? [1, C('default_page_total')];
        $resData = $this->noteService->getAssetRelationNoteList($param, $page, $request->getModuleCode());
        return json(success_response('', $resData));
    }

    /**
     * 批量导入需求
     * @param Request $request
     * @return Response
     * @throws \Throwable
     */
    public function importDemand(Request $request)
    {
        $param = $request->param();
        $type = $param['module_code'];
        $projectId = $param['project_id'];
        $data = $param['data'];

        $validate = new Note();
        $validate->scene('ImportDemandDataItem');
        foreach ($data as $item) {
            if (!$validate->check($item)) {
                throw_strack_exception($validate->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }

        $resData = $this->noteService->importDemand($data, $projectId, $type);
        return json(success_response('', $resData));
    }
}
